import os.path
import shutil
import re

# 遍历指定目录，显示目录下的所有文件名
def eachFile(filepath):
    pathDir = os.listdir(filepath)
    for allDir in pathDir:
        # child = os.path.join('%s\%s' % (filepath, allDir))
        child = os.path.join(filepath, allDir)
        if os.path.isfile(child):
            # print(child )
            getFile2(child,allDir)

            # readFile(child) 
        #     continue
        # eachFile(child)
# names: ['bus', 'suitcase','chair','dining table','bicycle','toilet','car', 'sink','boat','airplane','truck','tv','couch','refrigerator','oven','motorcycle','train','bed','microwave','toaster']  

# classes = ['chair','bicycle','car','motorbike','diningtable']  训练


def getFile2(filenames,pathname):
    flag=0
    f=open(filenames,'r')
    data=f.readlines()
    arr = []
    for i in data:
        a = i.split()
        arr.append(a[0])
        c = a[0]   # 读取该行的第一个数据
        # if (c in ['3','4','6','15']):2
        # if (c in ['3','2','4','15']):   6          # 修改---------------------------------------
        # if (c in ['3','2','4','6']):  15
        if (c in ['15','2','4','6']):      
            # print(c)
            # flag=True
            flag=1
    # print(arr)
    # if flag==False &  ('2'not in arr):
    #     # print(pathname)
    #     print(filenames)
    # if ('6' in arr) :             # 修改---------------------------------------
    if ('3' in arr) :      
        # print(pathname)
        if flag==0:

            print(filenames)
            getFile(filenames,pathname)
        # 
    f.close()

def getFile(filenames,pathname):
    f=open(filenames,'r')
    data=f.readlines()
    for i in data:

        # out_file.write(str(cls_id) + " " + " ".join(str(x) for x in bbox) + '\n')
        a = i.split()
        c = a[0]   # 读取该行的第一个数据

        if c=='3' :                 # 修改---------------------------------------

            save = "/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset3/coco5/all/diningtable/labels"     # 修改---------------------------------------
            save_path = os.path.join(save, pathname)

            file = open(save_path, 'a')
            # file.write(c+" " +a[1] +" "+a[2]+" "+a[3]+" "+a[4]+'\n')
            # file.write("3"+" " +a[1] +" "+a[2]+" "+a[3]+" "+a[4]+'\n')
            file.write("4"+" " +a[1] +" "+a[2]+" "+a[3]+" "+a[4]+'\n')
            # file.write(i)
            print(i)
            file.close()
        
        # print('***')
    f.close()    

if __name__ == "__main__":
    # filenames = './label10'  #标签根目录
    # filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/final_project/dataset/test/coco-20/images/train/labels'  #标签根目录
    filenames = '/dssg/home/zhineng_qt/test_2021_3_18_fu/final_project/dataset/test/coco-20/images/train/labels'  #标签根目录
    # /dssg/home/zhineng_qt/test_2021_3_18_fu/final_project/dataset/test/coco-20/images/train/labels
    # arr = []
    eachFile(filenames)